package top.hekun.study;

import com.jd.platform.async.executor.Async;
import com.jd.platform.async.executor.timer.SystemClock;
import com.jd.platform.async.wrapper.WorkerWrapper;
import lombok.SneakyThrows;

public class DemoTest {
    @SneakyThrows
    public static void main(String[] args) {
        WorkerDemo w0 = new WorkerDemo("0");
        WorkerDemo w1 = new WorkerDemo("1");
        WorkerDemo w2 = new WorkerDemo("2");
        WorkerWrapper<Integer, String> workerWrapper0 = new WorkerWrapper.Builder<Integer, String>()
                .worker(w0)
                .callback(w0)
                .param(1000)
                .build();

        WorkerWrapper<Integer, String> workerWrapper1 = new WorkerWrapper.Builder<Integer, String>()
                .worker(w1)
                .callback(w1)
                .param(2000)
                //  当前节点执行完即可执行 workerWrapper0 无需等待 workerWrapper2
                .next(workerWrapper0, false)
                .build();
        WorkerWrapper<Integer, String> workerWrapper2 = new WorkerWrapper.Builder<Integer, String>()
                .worker(w2)
                .callback(w2)
                .param(1000)
                //  当前节点执行完即可执行 workerWrapper0 无需等待 workerWrapper1
                .next(workerWrapper0, false)
                .build();


        long now = SystemClock.now();
        System.out.println("开始-" + now);

        Async.beginWork(5000, workerWrapper1, workerWrapper2);
//        Async.beginWork(800, workerWrapper, workerWrapper1, workerWrapper2);
//        Async.beginWork(1000, workerWrapper, workerWrapper1, workerWrapper2);

        System.out.println("结束-" + SystemClock.now());
        System.err.println("时间差-" + (SystemClock.now() - now));
        System.out.println("累积线程数-" + Async.getThreadCount());

        System.out.println(workerWrapper0.getWorkResult());
        System.out.println(workerWrapper1.getWorkResult());
        System.out.println(workerWrapper2.getWorkResult());
        Async.shutDown();
    }
}
